<?php
namespace module\tv\controller;

use think\Request;
use core\tv\model\StoreModel;
use core\tv\logic\StoreLogic;
use core\tv\validate\StoreValidate;
class Store extends Base
{
    /**
     * 门店列表
     * 
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request)
    {
        $this->siteTitle = '门店管理';

        $map = [];

        // 查询条件-状态
        $status = $request->param('status', '');
        if ($status != '') {
            $status = intval($status);
            $map['status'] = $status;
        }
        $this->assign('status', $status);

        // 查询条件-点播权限
        $is_ondemand = $request->param('is_ondemand', '');
        if ($is_ondemand != '') {
            $is_ondemand = intval($is_ondemand);
            $map['is_ondemand'] = $is_ondemand;
        }
        $this->assign('is_ondemand', $is_ondemand);

        // 查询条件-关键词
        $keyword = $request->param('keyword');
        if ($keyword != '') {
            $map['store_name'] = [
                'like',
                '%' . $keyword . '%'
            ];
        }
        $this->assign('keyword', $keyword);

        //列表
        $model = StoreModel::getInstance()->where($map)->order('sort DESC');
        $this->_page($model);

        //获取门店选择项
        $this->getStoreSelect();

        return $this->fetch();
    }

    /**
     * 新增门店
     * 
     * @param Request $request
     * @return mixed
     */
    public function add(Request $request)
    {
        if ($request->isPost()) {
            $data = [
                'store_name' => $request->param('store_name'),
                'province' => $request->param('province'),
                'city' => $request->param('city',''),
                'area' => $request->param('area'),
                'address' => $request->param('address'),
                'tel' => $request->param('tel'),
                'principal' => $request->param('principal'),
                'principal_phone' => $request->param('principal_phone'),
                'uuid' => $request->param('uuid',''),
                'group_id' => $request->param('group_id'),
                'is_ondemand' => $request->param('is_ondemand'),
                'status' => $request->param('status'),
                'sort' => $request->param('sort', 0),
                'remarks' => $request->param('remarks')
            ];

            //验证
            $this->_validate(StoreValidate::class, $data, 'common');

            //添加
            $this->_add(StoreModel::class, $data);
        } else {
            $this->siteTitle = '新增门店';

            //获取门店选择项
            $this->getStoreSelect();

            return $this->fetch();
        }
    }

    /**
     * 编辑门店
     * 
     * @param Request $request
     * @return mixed
     */
    public function edit(Request $request)
    {
        if ($request->isPost()) {
            $data = [
                'store_name' => $request->param('store_name'),
                'province' => $request->param('province'),
                'city' => $request->param('city',''),
                'area' => $request->param('area'),
                'address' => $request->param('address'),
                'tel' => $request->param('tel'),
                'principal' => $request->param('principal'),
                'principal_phone' => $request->param('principal_phone'),
                'uuid' => $request->param('uuid',''),
                'group_id' => $request->param('group_id'),
                'is_ondemand' => $request->param('is_ondemand'),
                'status' => $request->param('status'),
                'sort' => $request->param('sort', 0),
                'remarks' => $request->param('remarks')
            ];

            //验证
            $this->_validate(StoreValidate::class, $data, 'common');
            
            //修改
            $this->_edit(StoreModel::class, $data);
        } else {
            $this->siteTitle = '编辑门店';

            //记录
            $this->_record(StoreModel::class);

            //获取门店选择项
            $this->getStoreSelect();

            return $this->fetch();
        }
    }

    /**
     * 删除门店
     * 
     * @param Request $request
     * @return void
     */
    public function delete(Request $request)
    {
        $this->_delete(StoreModel::class, false);
    }

    /**
     * 修改字段值
     *
     * @param Request $request            
     * @return mixed
     */
    public function modify(Request $request)
    {
        $fields = [
            'sort',
            'status',
            'is_ondemand',
            'group_id'
        ];
        $this->_modify(StoreModel::class, $fields);
    }

    /**
     * 获取门店选择项
     * 
     * @return mixed
     */
    private function getStoreSelect()
    {
        //分组选择
        $storeGroup = StoreLogic::getInstance()->getStoreGroup();
        $this->assign('storeGroup',$storeGroup);

        //权限选择
        $isOndemand = StoreLogic::getInstance()->getIsOndemand();
        $this->assign('isOndemand',$isOndemand);

        //状态选择
        $storeStatus = StoreLogic::getInstance()->getStoreStatus();
        $this->assign('storeStatus',$storeStatus);
    }
    
}
